package com.axjy.controller;

import com.axjy.pojo.User;
import com.axjy.service.UserService;
import com.axjy.util.Result;
import com.axjy.vo.UserDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.HandlerAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/sys/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    private String msg;

    @GetMapping("/list")
    public Result list(Integer page, Integer limit, UserDto userDto){
       IPage pageResult=userService.queryAll(page,limit,userDto);
       return Result.success(pageResult);
    }

    @SneakyThrows
    @PostMapping("/uploadAttach")
    public Result uploadAttach(@RequestParam(value = "file") MultipartFile attach, HttpServletRequest req){
        if(!attach.isEmpty()){
            String path=req.getServletContext().getRealPath("/attach");//得到attach目录在web服务器的绝对路径
            String srcName= attach.getOriginalFilename();//得到原始文件名
            String suffix=FilenameUtils.getExtension(srcName);//得到原始文件后缀名
            String newFile= UUID.randomUUID().toString()+"."+suffix;//创建新的文件名
            File targetFile=new File(path,newFile);//构建文件对象
            if(!targetFile.exists()){
                targetFile.mkdirs();
            }
            System.out.println("上传路径:"+path);
            attach.transferTo(targetFile);
            Map data=new HashMap();
            data.put("attachPath","attach/"+newFile);
            return Result.success(data);
        }

        return Result.fail("上传失败");
    }

    @PostMapping("/updateAttach")
    public Result updateAttach(@RequestBody User user){
       try {
           userService.updateAttach(user);
           return Result.success("用户上传证件成功!");
       }catch (Exception ex){;
           String msg="用户修改attach失败!";
           log.error(msg,ex);
           return Result.fail(msg);
       }
    }

    @PostMapping("/add")
    public Result add(User user){
        try{
            userService.addUser(user);
            return Result.success("用户添加成功!");
        }catch (Exception ex){
            msg="添加用户失败!";
            log.error(msg,ex);
            return  Result.fail(msg);
        }
    }

    @PostMapping("/update")
    public Result update(User user){
        try{
            userService.updateUser(user);
            return Result.success("用户修改成功!");
        }catch (Exception ex){
            msg="用户修改失败!";
            log.error(msg,ex);
            return  Result.fail(msg);
        }
    }

    @GetMapping("/batchDel")
    public Result batchDel(Integer[] ids){
        try{
            userService.batchDel(ids);
            return Result.success("批量删除成功!");
        }catch (Exception ex){
            msg="批量删除失败!";
            log.error(msg,ex);
            return Result.fail(msg);
        }
    }

    @GetMapping("/del")
    public Result del(Integer id){
        try{
            userService.deleteById(id);
            return Result.success("删除成功!");
        }catch (Exception ex){
            msg="删除失败!";
            log.error(msg,ex);
            return Result.fail(msg);
        }
    }


}
